home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 January / macformat46.iso / Shareware Plus / Developers / Library / Grant's CGI Framework / Grant's CGI Framework / Project Files / MyCGIProcess.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-28  |  7.6 KB  |  228 lines

  1. /*****
  2.  *
  3.  *    Grant's CGI Shell (Common Grant Interface :-)
  4.  *
  5.  *    MyCGIProcess.c
  6.  *
  7.  *    Sample application using the cgi interface.
  8.  *
  9.  *    CustomCGIProcess is where you will do your application specific processing
  10.  *  of the cgi stuff.
  11.  *
  12.  *    by Grant Neufeld
  13.  *
  14.  *    Copyright ©1995,1996 by Grant Neufeld
  15.  *
  16.  *    http://arpp.carleton.ca/cgi/framework/
  17.  *    gneufeld@ccs.carleton.ca
  18.  *    grant@acm.org
  19.  *
  20.  *    The copyright notice must be kept in this source.
  21.  *    I ask that you let me know of any enhancements (read: bug fixes) to this code.
  22.  *    I would also like copies of (or discounts on) anything you produce using this code, please.
  23.  *
  24.  *****/
  25.  
  26. #include "MyConfiguration.h"
  27. #if kCompileWithCGICode
  28.  
  29. #include <string.h>
  30. #include <Threads.h>
  31.  
  32. #include "globals.h"
  33.  
  34. #include "CGI.h"
  35. #include "CustomHandlers.h"
  36. #include "DebugUtil.h"
  37. #include "LogUtil.h"
  38. #include "MemoryUtil.h"
  39. #include "ProcessUtil.h"
  40.         
  41.  
  42. /***  CUSTOM CGI FUNCTION  ***/
  43.  
  44. /* This function is where the CGI is actually processed.
  45.     You should replace its contents with your own.
  46.     You need to allocate (*theCGIHdl)->responseData using CGINewHandle.
  47.     Put an HTTP header at the beginning of (*theCGIHdl)->responseData.
  48.     Put your data immediately following the last character of the HTTP header.
  49.     (*theCGIHdl)->responseData will be automatically deallocated by the CGI handler.
  50.     You should set (*theCGIHdl)->responseSize to be the size of the responseData,
  51.      including null terminator if you have one.
  52.     
  53.     (this function's prototype is defined in CustomHandlers.h) */
  54. void
  55. CustomCGIProcess ( CGIHdl theCGIHdl )
  56. {
  57.     char *    myResponseData;
  58.     
  59.     /* debugging check to ensure that theCGIHdl is not NULL */
  60.     my_assert ( theCGIHdl != NULL,
  61.         "\pCustomCGIProcess: theCGIHdl is NULL" );
  62.     
  63. #define kHelloWorldStr "<HTML><HEAD><TITLE>Hello</TITLE></HEAD><BODY><P>Hello, World!</P></BODY></HTML>"
  64.     
  65.     /* gHTTPHeaderOKSize is the size of the default OK HTTP header that you need
  66.         to prepend to your data.
  67.         The extra 1 at the end is for the string's null terminator. */
  68.     (*theCGIHdl)->responseSize = gHTTPHeaderOKSize + strlen(kHelloWorldStr) + 1;
  69.     
  70.     /* allocate the responseData */
  71.     (*theCGIHdl)->responseData = CGINewHandle ( theCGIHdl, (*theCGIHdl)->responseSize, nil );
  72.     if ( (*theCGIHdl)->responseData != NULL )
  73.     {
  74.         /* lock theCGIHdl so we can work with its data not moving */
  75.         HLock ( (*theCGIHdl)->responseData );
  76.         /* get a direct pointer to the responseData to make things a little easier */
  77.         myResponseData = *((*theCGIHdl)->responseData);
  78.  
  79.         /* copy the default HTTP OK header to the begining of our data */
  80.         strcpy ( myResponseData, (char *)gHTTPHeaderOK );
  81.         /* copy our data - "Hello, World!" */
  82.         strcpy ( &(myResponseData[gHTTPHeaderOKSize]), kHelloWorldStr );
  83.  
  84.         /* don't leave theCGIHdl locked when done */
  85.         HUnlock ( (*theCGIHdl)->responseData );
  86.     }
  87.     else
  88.     {
  89.         /* unable to allocate data - framework will return proper http error header */
  90.         (*theCGIHdl)->responseSize = nil;
  91.     }
  92.     
  93.     /* you should try to make use of giving time,
  94.         especially in for and while loops or where you have to wait for
  95.         some other process to return data or finish a task. */
  96.     ProcessGiveTime ( nil, true, theCGIHdl );
  97. } /* CustomCGIProcess */
  98.  
  99.  
  100. /* this function will be called after the cgi result has been returned.
  101.     It will contain the same CGIHdl that was used for the CustomCGIProcess.
  102.     This function's prototype is defined in "CustomHandlers.h" */
  103. void
  104. CustomCGIPostProcess ( CGIHdl theCGIHdl )
  105. {
  106.     /* as an example, I'll log some info here */
  107.     LogStringBreakP ( "\pHere is a post-process log entry:" );
  108.     
  109.     #if kCompileWithCGIfull_request
  110.     /* now I'll try to log the full_request, if it's present */
  111.     if ( (*theCGIHdl)->full_request != NULL && ((*theCGIHdl)->full_request)[nil] != nil )
  112.     {
  113. //•••        LogStringP ( "\p\tfull_request: " );
  114. //•••        LogStringBreak ( (*theCGIHdl)->full_request );
  115.     }
  116.     #endif
  117. } /* CustomCGIPostProcess */
  118.  
  119.  
  120. //••• this function has been removed
  121. //••• use 'CustomStartup' in "MyHandlers.c" instead.
  122. //
  123. ///***  CUSTOM CGI INITIALIZATION  ***/
  124. //#pragma segment Startup
  125. //
  126. ///* Put any of the initialization you need done, here.
  127. //    This function will be called once: in-between the startup
  128. //    sequence and the main event loop.
  129. //    Return true if the initialization was successful, otherwise false.
  130. //    An initialization failure will result in the application quitting. */
  131. //Boolean
  132. //CustomCGIStartup ( void )
  133. //{
  134. //    return true;
  135. //} /* CustomCGIStartup */
  136.  
  137.  
  138. //••• this function has been removed
  139. //••• use 'CustomQuit' in "MyHandlers.c" instead.
  140. //
  141. ///***  CUSTOM CGI CLEAN UP  ***/
  142. //#pragma segment Main
  143. //
  144. ///* This function is called at quitting time. Put any cleanup you need to do in it.
  145. //    Return true if you are succeful.
  146. //    Return false if you are unable to quit for some reason (this generally only
  147. //    applies when the user cancels)
  148. //    allowUserInteract specifies whether you can make user interface calls.
  149. //    However, CGIs generally should not rely on any user interface calls, so don't
  150. //    depend on them. */
  151. //Boolean
  152. //CustomCGIQuit ( Boolean allowUserInteract )
  153. //{
  154. //#pragma unused (allowUserInteract)
  155. //    return true;
  156. //}/* CustomCGIQuit */
  157.  
  158.  
  159. /***  WSAPI SUPPORT  ***/
  160. #if kCompilingForWSAPI
  161.  
  162. #define kFileCreatorTypeAny        '*   '
  163.  
  164. #define kMyPluginAction        "GRANTSCGI"
  165. #define kMyFileExtension    ".GRANT"
  166.  
  167. /* This function is called when the WSAPI_Register event is received.
  168.     You can not do anything other than registering actions and suffixes,
  169.     and identifying this plug-in's capabilities.
  170.     See the WSAPI documentation [section 2.2.2] for further details. */
  171. WSAPI_ErrorCode
  172. CustomCGIWSAPIRegister ( WSAPI_CommandPBPtr commandPtr )
  173. {
  174.     WSAPI_ErrorCode    theErr;
  175.     
  176.     /* return plug-in name and abilities, register actions and suffixes. */
  177.     
  178.     /* your code must make at least one of the following WSAPI Register
  179.         calls or there's no way for WebSTAR to pass CGI requests to your plug-in */
  180.     
  181.     /* in this example, we're registering "GRANTSCGI" as an action.
  182.         You should always use the kMyCGIName constant (don't forget to properly
  183.         set it in "MyConfiguration.h". */
  184.     theErr = WSAPI_RegisterAction ( commandPtr, kMyPluginAction, kMyCGIName );
  185.     if ( theErr != WSAPI_I_NoErr )
  186.     {
  187.         /* PlugIn Action didn't register. Notify user.
  188.             May want to take some action other than just
  189.             displaying this message */
  190.         WSAPI_DisplayMessage ( commandPtr, kMyCGIName ": Couldn't register the action." );
  191.     }
  192.     
  193.     /* in this example, we're registering ".GRANTSCGI" as a suffix mapping for
  194.         the "GRANTSCGI" action (registered above). We're also registering it
  195.         for files that have the TEXT filetype and CGI? as their creator type.
  196.         If you don't care about particular file or creator types, then just
  197.         use '*   ' (defined as kFileCreatorTypeAny directly above this function). */
  198.     theErr = WSAPI_RegisterSuffix ( commandPtr, kMyPluginAction, kMyFileExtension,
  199.         kFileCreatorTypeAny, kFileCreatorTypeAny, "text/html" );
  200.         //'TEXT', 'CGI?', "text/html" );
  201.     if ( theErr != WSAPI_I_NoErr )
  202.     {
  203.         /* PlugIn Suffix didn't register. Notify user.
  204.             May want to take some action other than just
  205.             displaying this message */
  206.         WSAPI_DisplayMessage ( commandPtr, kMyCGIName ": Couldn't register the suffix." );
  207.     }
  208.     
  209.     /* you need to tell the server which capabilities you support. The required
  210.         constants are defined in <WSAPI.h>. Add the appropriate ones together
  211.         and store their sum in the capabilities field of the commandPtr.
  212.             capCGI                - able to perform the standard CGI role
  213.             capPreProcessor        - able to perform the preprocessor role
  214.             capPostProcessor    - able to perform the postprocessor role
  215.             capLogging            - can handle logging and status messages
  216.             capSecurity            - able to perform the security role */
  217.     commandPtr->param.init.capabilities = capCGI;
  218.     
  219.     return theErr;
  220. } /* CustomCGIWSAPIRegister */
  221.  
  222. #endif /* kCompilingForWSAPI */
  223.  
  224.  
  225. #endif    /* kCompileWithCGICode */
  226.  
  227. /*** EOF ***/
  228.